package com.badibadi.adapter;

import java.util.List;

import net.tsz.afinal.FinalBitmap;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.badibadi.activity.ActivityReadingActivity;
import com.badibadi.activity.PinyinSeeActivityTheOriginatorLookParticipantsActivity;
import com.badibadi.infos.Activity_Overview_Model;
import com.badibadi.mytools.CalendarTools;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Utils;
import com.badibadi.mytools.ViewHolder;
import com.badibadi.uniclubber.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * 活动阅读中 相关活动的适配器
 * 
 * @author Administrator
 * 
 */
public class LinkActivityAdapter extends BaseAdapter {
	private DisplayImageOptions options;
	private List<Activity_Overview_Model> activity_Overview_Models;
	private Context context;
	FinalBitmap finalBitmap;
	private int Type;

	public LinkActivityAdapter(
			List<Activity_Overview_Model> activity_Overview_Models,
			Context context, int t) {
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.noimage)
				.showImageOnFail(R.drawable.noimage).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5)).build();
		this.activity_Overview_Models = activity_Overview_Models;
		this.context = context;
		Type = t;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return activity_Overview_Models.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return activity_Overview_Models.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub fragment_myrl_import_type_name
		if (convertView == null) {
			convertView = ViewHolder.buildView(context,
					R.layout.activity_over_view_adapter_kanhuodong, parent);
		}
		ViewHolder viewHolder = (ViewHolder) convertView.getTag();

		final ImageView fragment_myrl_import_background_img = viewHolder
				.getViewById(R.id.fragment_myrl_import_background_img);// 背景
		ImageView fragment_myrl_import_type_chakan = viewHolder
				.getViewById(R.id.fragment_myrl_import_type_chakan);// 查看点击事件图片
		ImageView fragment_myrl_import_user_img1 = viewHolder
				.getViewById(R.id.fragment_myrl_import_user_img1);// 个人头像背景1
		ImageView fragment_myrl_import_user_img2 = viewHolder
				.getViewById(R.id.fragment_myrl_import_user_img2);// 个人头像背景2
		ImageView fragment_myrl_import_user_img3 = viewHolder
				.getViewById(R.id.fragment_myrl_import_user_img3);// 个人头像背景3
		ImageView fragment_myrl_import_user_img4 = viewHolder
				.getViewById(R.id.fragment_myrl_import_user_img4);// 个人头像背景4
		ImageView fragment_myrl_import_user_img5 = viewHolder
				.getViewById(R.id.fragment_myrl_import_user_img5);// 个人头像背景5
		ImageView fragment_myrl_import_user_img6 = viewHolder
				.getViewById(R.id.fragment_myrl_import_user_img6);// 个人头像背景6
		TextView fragment_myrl_import_adress_textview = viewHolder
				.getViewById(R.id.fragment_myrl_import_adress_textview);// 活动名称
		TextView f_m_i_l_starttime = viewHolder
				.getViewById(R.id.f_m_i_l_starttime);// 开始时间
		TextView f_m_i_l_overtime = viewHolder
				.getViewById(R.id.f_m_i_l_overtime);// 结束时间
		TextView f_m_i_l_name = viewHolder.getViewById(R.id.f_m_i_l_name);// 线上主题
		TextView fragment_myrl_import_instance = viewHolder
				.getViewById(R.id.fragment_myrl_import_instance);// 距离(显示附近适用)
		TextView fragment_myrl_import_type_name = viewHolder
				.getViewById(R.id.fragment_myrl_import_type_name);// 类型fragment_myrl_import_type_name
		TextView fragment_myrl_import_user_more = viewHolder
				.getViewById(R.id.fragment_myrl_import_user_more);// 查看更多
		fragment_myrl_import_adress_textview.setText(activity_Overview_Models
				.get(position).getNames());
		LinearLayout see_canyuzhe = viewHolder.getViewById(R.id.see_canyuzhe);
		// 看参与者
		see_canyuzhe.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Intent intent=new
				// Intent(context,PinyinTheOriginatorLookParticipantsActivity.class);
				// intent.putExtra("aid", aid);
				// intent.putExtra("fq_uid", fq_uid);
				// context.startActivity(intent);
				if (!Utils.getUid(context).equals("")) {
					Intent intent = new Intent(
							context,
							PinyinSeeActivityTheOriginatorLookParticipantsActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.putExtra("aid",
							activity_Overview_Models.get(position).getId());
					if (activity_Overview_Models.get(position).getA_uid() != null) {
						intent.putExtra("fq_uid",
								activity_Overview_Models.get(position)
										.getA_uid());
					} else {
						intent.putExtra("fq_uid",
								activity_Overview_Models.get(position).getUid());
					}
					context.startActivity(intent);
				} else {
					Toast.makeText(context,
							context.getResources().getString(R.string.l_xb10), 0)
							.show();
				}
			}
		});

		f_m_i_l_starttime.setText(""
				+ CalendarTools.getFormatTime(
						Double.parseDouble(activity_Overview_Models.get(
								position).getStart_time()
								+ "000"), "yyyy.MM.dd"));
		f_m_i_l_overtime.setText(""
				+ CalendarTools.getFormatTime(
						Double.parseDouble(activity_Overview_Models.get(
								position).getStart_time()
								+ "000"), "yyyy.MM.dd"));
		fragment_myrl_import_type_name.setText(""
				+ activity_Overview_Models.get(position).getTypeName());
		if (activity_Overview_Models.get(position).getIs_online().equals("1")) {
			f_m_i_l_name.setText(context.getResources().getString(
					R.string.OnlineTheme));
			// 线上主题点击后进入线上的
			fragment_myrl_import_background_img
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							ActivityReadingActivity activity = new ActivityReadingActivity();
							Intent intent = new Intent(context,
									ActivityReadingActivity.class);
							intent.putExtra("actId", activity_Overview_Models
									.get(position).getId());
							intent.putExtra("isOnline", true);
							context.startActivity(intent);
							((Activity) (context)).finish();
						}
					});

		} else if (activity_Overview_Models.get(position).getIs_online()
				.equals("0")) {
			f_m_i_l_name.setText(""
					+ activity_Overview_Models.get(position).getAddress());
			fragment_myrl_import_background_img
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							// 进入到线下的
							ActivityReadingActivity activity = new ActivityReadingActivity();
							Intent intent = new Intent(context,
									ActivityReadingActivity.class);
							intent.putExtra("actId", activity_Overview_Models
									.get(position).getId());
							intent.putExtra("isOnline", false);
							context.startActivity(intent);
							((Activity) (context)).finish();
						}
					});
		}

		if (Type != 3) {
			fragment_myrl_import_instance.setVisibility(View.GONE);
		}
		// 背景
		ImageLoader.getInstance().displayImage(
				Constants.BadiDownImgUrl
						+ activity_Overview_Models.get(position).getImage()
						+ Constants.Appactivitycover,
				fragment_myrl_import_background_img, options);
		// fragment_myrl_import_background_img
		// .setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// // Intent intent = new Intent(context,
		// // ActivityReadingActivity.class);
		// // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// // intent.putExtra("actId",
		// // activity_Overview_Models.get(position).getId());
		// // context.startActivity(intent);
		// }
		// });

		if (activity_Overview_Models.get(position).getUser() != null
				&& activity_Overview_Models.get(position).getUser().size() > 0) {
			for (int i = 0; i < activity_Overview_Models.get(position)
					.getUser().size(); i++) {
				switch (i) {
				case 0:
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ activity_Overview_Models.get(position)
											.getUser().get(0).getHead()
									+ Constants.appPhoto4img,
							fragment_myrl_import_user_img1, options);
					break;
				case 1:
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ activity_Overview_Models.get(position)
											.getUser().get(1).getHead()
									+ Constants.appPhoto4img,
							fragment_myrl_import_user_img2, options);
					break;
				case 2:
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ activity_Overview_Models.get(position)
											.getUser().get(2).getHead()
									+ Constants.appPhoto4img,
							fragment_myrl_import_user_img3, options);
					break;
				case 3:
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ activity_Overview_Models.get(position)
											.getUser().get(3).getHead()
									+ Constants.appPhoto4img,
							fragment_myrl_import_user_img4, options);
					break;
				case 4:
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ activity_Overview_Models.get(position)
											.getUser().get(4).getHead()
									+ Constants.appPhoto4img,
							fragment_myrl_import_user_img5, options);
					break;
				case 5:
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ activity_Overview_Models.get(position)
											.getUser().get(5).getHead()
									+ Constants.appPhoto4img,
							fragment_myrl_import_user_img6, options);
					break;

				default:
					break;
				}
			}
			if (activity_Overview_Models.get(position).getUser().size() <= 5) {
				fragment_myrl_import_user_img2.setVisibility(View.VISIBLE);
				fragment_myrl_import_user_img3.setVisibility(View.VISIBLE);
				fragment_myrl_import_user_img4.setVisibility(View.VISIBLE);
				fragment_myrl_import_user_img5.setVisibility(View.VISIBLE);
				fragment_myrl_import_user_img6.setVisibility(View.VISIBLE);
				fragment_myrl_import_user_more.setVisibility(View.GONE);
			}
			if (activity_Overview_Models.get(position).getUser().size() <= 1) {
				fragment_myrl_import_user_img2.setVisibility(View.GONE);
				fragment_myrl_import_user_img3.setVisibility(View.GONE);
				fragment_myrl_import_user_img4.setVisibility(View.GONE);
				fragment_myrl_import_user_img5.setVisibility(View.GONE);
				fragment_myrl_import_user_img6.setVisibility(View.GONE);
				fragment_myrl_import_user_more.setVisibility(View.GONE);
			} else if (activity_Overview_Models.get(position).getUser().size() <= 2) {
				fragment_myrl_import_user_img2.setVisibility(View.VISIBLE);
				fragment_myrl_import_user_img3.setVisibility(View.GONE);
				fragment_myrl_import_user_img4.setVisibility(View.GONE);
				fragment_myrl_import_user_img5.setVisibility(View.GONE);
				fragment_myrl_import_user_img6.setVisibility(View.GONE);
				fragment_myrl_import_user_more.setVisibility(View.GONE);
			} else if (activity_Overview_Models.get(position).getUser().size() <= 3) {
				fragment_myrl_import_user_img2.setVisibility(View.VISIBLE);
				fragment_myrl_import_user_img3.setVisibility(View.VISIBLE);
				fragment_myrl_import_user_img4.setVisibility(View.GONE);
				fragment_myrl_import_user_img5.setVisibility(View.GONE);
				fragment_myrl_import_user_img6.setVisibility(View.GONE);
				fragment_myrl_import_user_more.setVisibility(View.GONE);
			} else if (activity_Overview_Models.get(position).getUser().size() <= 4) {
				fragment_myrl_import_user_img2.setVisibility(View.VISIBLE);
				fragment_myrl_import_user_img3.setVisibility(View.VISIBLE);
				fragment_myrl_import_user_img4.setVisibility(View.VISIBLE);
				fragment_myrl_import_user_img5.setVisibility(View.GONE);
				fragment_myrl_import_user_img6.setVisibility(View.GONE);
				fragment_myrl_import_user_more.setVisibility(View.GONE);
			} else if (activity_Overview_Models.get(position).getUser().size() < 5) {
				fragment_myrl_import_user_img2.setVisibility(View.VISIBLE);
				fragment_myrl_import_user_img3.setVisibility(View.VISIBLE);
				fragment_myrl_import_user_img4.setVisibility(View.VISIBLE);
				fragment_myrl_import_user_img5.setVisibility(View.VISIBLE);
				fragment_myrl_import_user_img6.setVisibility(View.GONE);
				fragment_myrl_import_user_more.setVisibility(View.GONE);
			}
		} else {
			fragment_myrl_import_user_img1.setVisibility(View.GONE);
			fragment_myrl_import_user_img2.setVisibility(View.GONE);
			fragment_myrl_import_user_img3.setVisibility(View.GONE);
			fragment_myrl_import_user_img4.setVisibility(View.GONE);
			fragment_myrl_import_user_img5.setVisibility(View.GONE);
			fragment_myrl_import_user_img6.setVisibility(View.GONE);
			fragment_myrl_import_user_more.setVisibility(View.GONE);
		}

		return convertView;
	}
}
